package Model.Core.Impl;

import Factory.Entities.LinkFactory;
import Factory.Tip.TipsFactory;
import Model.Core.IHandleEntity;
import Model.Core.ICommunicatorEntity;
import Model.Core.Tip.Tip;
import Model.Core.Tip.TipMoment;
import Model.Core.Tip.TipType;
import Model.Draw.Events.ManifestEntityDoneListener;
import java.util.ArrayList;
import java.util.List;

/**
 * Projeto de TCC: Editor de Diagramas de Comunicação em UML.
 * @author Jose Luz
 */
public class LinkEntity implements IHandleEntity {

    private ICommunicatorEntity objectSource;
    private ObjectEntity objectTarget;
    private List<MessageEntity> messageEntity;
    private LinkFactory linkFactory;
    private List<Tip> tips;

    public LinkEntity() {
        messageEntity = new ArrayList<MessageEntity>();
        linkFactory = new LinkFactory();
        tips = TipsFactory.createLinkTips();
    }

    @Override
    public void manifestGraphically(Object sourceElement, Object targetElement) {
        linkFactory.createEntityGraphicalManifest(this, sourceElement, targetElement);
    }

    @Override
    public void addGraphicalManifestListener(ManifestEntityDoneListener listener) {
        linkFactory.addListener(listener);
    }

    @Override
    public void removeGraphicalManifestListener(ManifestEntityDoneListener listener) {
        linkFactory.removeListener(listener);
    }

    @Override
    public void select() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Tip findTipBy(TipType type, TipMoment moment) {
        for (Tip tip : tips) {
            if (tip.getType() == type && tip.getMoment() == moment) {
                return tip;
            }
        }
        return null;
    }

    public ObjectEntity getObjectTarget() {
        return objectTarget;
    }

    public void setObjectTarget(ObjectEntity objectTarget) {
        this.objectTarget = objectTarget;
    }

    public ICommunicatorEntity getObjectSource() {
        return objectSource;
    }

    public void setObjectSource(ICommunicatorEntity objectSource) {
        this.objectSource = objectSource;
    }

    public void addMessageEntity(MessageEntity message) {
        this.messageEntity.add(message);
    }

    public void removeMessageEntity(MessageEntity message) {
        this.messageEntity.remove(message);
    }

    public int getQuantityMessages(){
        return messageEntity.size();
    }
     
    public int getIndexOf(MessageEntity link){
        if(!messageEntity.contains(link))
            return -1;
        else{
            return messageEntity.indexOf(link);
        }
    }
    
    @Override
    public String toString() {
        return "Link Entity";
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final LinkEntity other = (LinkEntity) obj;
        if (this.objectSource != other.objectSource && (this.objectSource == null || !this.objectSource.equals(other.objectSource))) {
            return false;
        }
        if (this.objectTarget != other.objectTarget && (this.objectTarget == null || !this.objectTarget.equals(other.objectTarget))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 11 * hash + (this.objectSource != null ? this.objectSource.hashCode() : 0);
        hash = 11 * hash + (this.objectTarget != null ? this.objectTarget.hashCode() : 0);
        return hash;
    }

    @Override
    public String getDescription() {
        return "";
    }

    @Override
    public void setDescription(String description) {
        
    }
}